package me.flyray.crm.core.biz;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.dromara.hmily.annotation.Hmily;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.wxiaoqi.security.auth.common.util.SnowFlake;
import com.github.wxiaoqi.security.biz.common.crm.request.FreezeRequest;
import com.github.wxiaoqi.security.biz.common.crm.request.IntoAccountRequest;
import com.github.wxiaoqi.security.biz.common.crm.request.OutAccountRequest;
import com.github.wxiaoqi.security.biz.common.crm.request.UnFreezeAndOutAccountRequest;
import com.github.wxiaoqi.security.biz.common.crm.request.UnFreezeAndTransferRequest;
import com.github.wxiaoqi.security.biz.common.crm.request.UnFreezeRequest;
import com.github.wxiaoqi.security.biz.common.msg.BizResponseCode;
import com.github.wxiaoqi.security.common.enums.AccountStateEnums;
import com.github.wxiaoqi.security.common.enums.CustomerTypeEnums;
import com.github.wxiaoqi.security.common.enums.InOutFlagEnums;
import com.github.wxiaoqi.security.common.exception.BusinessException;
import com.github.wxiaoqi.security.common.msg.BaseApiResponse;
import com.github.wxiaoqi.security.common.util.CurrencyUtils;
import com.github.wxiaoqi.security.common.util.EntityUtils;
import com.github.wxiaoqi.security.common.util.MD5;

import lombok.extern.slf4j.Slf4j;
import me.flyray.crm.core.entity.MerchantAccount;
import me.flyray.crm.core.entity.MerchantAccountJournal;
import me.flyray.crm.core.entity.PersonalAccount;
import me.flyray.crm.core.entity.PersonalAccountJournal;
import me.flyray.crm.core.entity.UnfreezeJournal;
import me.flyray.crm.core.mapper.MerchantAccountJournalMapper;
import me.flyray.crm.core.mapper.MerchantAccountMapper;
import me.flyray.crm.core.mapper.PersonalAccountJournalMapper;
import me.flyray.crm.core.mapper.PersonalAccountMapper;
import me.flyray.crm.core.mapper.UnfreezeJournalMapper;

/**
 * 个人或者商户解冻并出账接口
 */
@Slf4j
@Transactional(rollbackFor = Exception.class)
@Service
public class CommonOutAccountBizTcc {
	@Value("${bountyHunter.balanceSaltValue}")
	private String balanceSaltValue;
	@Autowired
	private MerchantAccountJournalMapper merchantAccountJournalMapper;
	@Autowired
	private MerchantAccountMapper merchantAccountMapper;
	@Autowired
	private UnfreezeJournalMapper unfreezeJournalMapper;
	@Autowired
	private PersonalAccountMapper personalAccountMapper;
	@Autowired
	private PersonalAccountJournalMapper personalAccountJournalMapper;
	@Autowired
	private CommonIntoAccountBiz commonIntoAccountBiz;
	/**
	 * 个人或者商户解冻并出账接口
	 * 若是商户：
	 * 1.商户账户冻结金额扣除
	 * 若是个人：
	 * 1.个人账户冻结金额扣除
	 */
	@Hmily(confirmMethod="unFreAndOutAccountConfirm",cancelMethod="unFreAndOutAccountCancel")
	public BaseApiResponse unFreAndOutAccount(UnFreezeAndOutAccountRequest unFreAndOutAccountRequest) {
		log.info("try 个人或者商户解冻并出账接口-请求参数：{}" + EntityUtils.beanToMap(unFreAndOutAccountRequest));
		BaseApiResponse response = new BaseApiResponse();
		try {
			if(unFreAndOutAccountRequest.getCustomerType().equals(CustomerTypeEnums.CUST_MERCHANT.getCode())){
				MerchantAccount queryMerchantAccount=new MerchantAccount();
				queryMerchantAccount.setPlatformId(unFreAndOutAccountRequest.getPlatformId());
				queryMerchantAccount.setMerchantId(unFreAndOutAccountRequest.getMerchantId());
				queryMerchantAccount.setMerchantType((unFreAndOutAccountRequest.getMerchantType()));
				queryMerchantAccount.setAccountType(unFreAndOutAccountRequest.getAccountType());
				MerchantAccount merchantAccount= merchantAccountMapper.selectOne(queryMerchantAccount);
				if(null!=merchantAccount&&merchantAccount.getStatus().equals(AccountStateEnums.normal.getCode())){
					if (!MD5.sign(CurrencyUtils.decimaltoString(merchantAccount.getAccountBalance()), balanceSaltValue, "utf-8").equals(merchantAccount.getCheckSum())) {
						response.setCode( BizResponseCode.MER_ACC_BALANCE_NOT_MATE.getCode());
						response.setMessage( BizResponseCode.MER_ACC_BALANCE_NOT_MATE.getMessage());
						log.info("个人或者商户解冻并出账接口-返回参数。。。。。。{}", response);
						return response;
					}
					if(merchantAccount.getFreezeBalance().compareTo(new BigDecimal(unFreAndOutAccountRequest.getUnFreezeAmt()))<0){//商户账户余额不足
						response.setCode( BizResponseCode.MER_ACC_BALANCE_NOT_ENOUGH.getCode());
						response.setMessage( BizResponseCode.MER_ACC_BALANCE_NOT_ENOUGH.getMessage());
						log.info("个人或者商户解冻并出账接口-返回参数。。。。。。{}", response);
						return response;
					}
					
					merchantAccount.setFreezeBalance(merchantAccount.getFreezeBalance().subtract(new BigDecimal(unFreAndOutAccountRequest.getUnFreezeAmt())));
//					merchantAccount.setCheckSum(MD5.sign(merchantAccount.getAccountBalance().toString(), balanceSaltValue, "utf-8"));
					merchantAccountMapper.updateByPrimaryKeySelective(merchantAccount);
				}else{
					log.info("个人或者商户解冻并出账接口--商户账户不存在或者被账户被冻结");
					response.setCode( BizResponseCode.MER_ACC_NOTEXIST.getCode());
					response.setMessage( BizResponseCode.MER_ACC_NOTEXIST.getMessage());
					return response;
				}
				response.setData(merchantAccount.getAccountId());
			}
			/**
			 * 客户类型-若是个人 : 则需要个人账户余额增加、新增个人账户流水记录
			 * */
			if(unFreAndOutAccountRequest.getCustomerType().equals(CustomerTypeEnums.CUST_PERSONAL.getCode())){
				PersonalAccount queryPersonalAccount = new PersonalAccount();
				queryPersonalAccount.setPlatformId(unFreAndOutAccountRequest.getPlatformId());
				queryPersonalAccount.setPersonalId(unFreAndOutAccountRequest.getPersonalId());
				queryPersonalAccount.setAccountType(unFreAndOutAccountRequest.getAccountType());
				PersonalAccount personalAccount = personalAccountMapper.selectOne(queryPersonalAccount);
				if(null!=personalAccount&&personalAccount.getStatus().equals(AccountStateEnums.normal.getCode())){
					if(!MD5.sign(CurrencyUtils.decimaltoString(personalAccount.getAccountBalance()), balanceSaltValue, "utf-8").equals(personalAccount.getCheckSum())){
						log.info("个人或者商户解冻并出账接口--个人账户余额不匹配");
						response.setCode( BizResponseCode.PER_ACC_BALANCE_NOT_MATE.getCode());
						response.setMessage( BizResponseCode.PER_ACC_BALANCE_NOT_MATE.getMessage());
						log.info("个人或者商户解冻并出账接口。。。。。。{}", response);
						return response;
					}
					personalAccount.setFreezeBalance(personalAccount.getFreezeBalance().subtract(new BigDecimal(unFreAndOutAccountRequest.getUnFreezeAmt())));
					personalAccountMapper.updateByPrimaryKeySelective(personalAccount);
				}else{
					log.info("个人或者商户解冻并出账接口--个人账户不存在或者被冻结");
					response.setCode( BizResponseCode.PER_ACC_NOTEXIST.getCode());
					response.setMessage( BizResponseCode.PER_ACC_NOTEXIST.getMessage());
					return response;
				}
				response.setData(personalAccount.getAccountId());
			}
			response.setCode( BizResponseCode.OK.getCode());
			response.setMessage( BizResponseCode.OK.getMessage());
		} catch (Exception e) {
			e.printStackTrace();
			log.info("个人或者商户解冻并出账接口-报错--"+e.getMessage());
			response.setCode( BizResponseCode.SERVICE_NOT_AVALIABLE.getCode());
			response.setMessage( BizResponseCode.SERVICE_NOT_AVALIABLE.getMessage());
			return response;
		}
		log.info("try 个人或者商户解冻并出账接口-结束：{}" + response);
		return response;
	}
	
	/**
	 * 解冻接口接口 confirm 不做任何操作
	 * @param unFreAndOutAccountRequest
	 * @return
	 */
	public BaseApiResponse unFreAndOutAccountConfirm(UnFreezeAndOutAccountRequest unFreAndOutAccountRequest){
		log.info("confirm 个人或者商户解冻并出账接口-请求参数：{}" + EntityUtils.beanToMap(unFreAndOutAccountRequest));
		BaseApiResponse response = new BaseApiResponse();
		response.setCode( BizResponseCode.OK.getCode());
		response.setMessage( BizResponseCode.OK.getMessage());
		log.info("confirm 个人或者商户解冻并出账接口-结束：{}" + response);
		return response;
	}
	
	/**
	 * 解冻接口 cancel
	 * 个人或者商户解冻并出账回滚
	 * 若是商户：
	 * 1.商户账户冻结金额增加
	 * 若是个人：
	 * 1.个人账户冻结金额增加
	 */
	public BaseApiResponse unFreAndOutAccountCancel(UnFreezeAndOutAccountRequest unFreAndOutAccountRequest){
		log.info("cancel 个人或者商户解冻并出账接口-请求参数：{}" + EntityUtils.beanToMap(unFreAndOutAccountRequest));
		BaseApiResponse response = new BaseApiResponse();
		/**
		 * 根据冻结流水号查询是否被解冻
		 * */
		try {
			if(unFreAndOutAccountRequest.getCustomerType().equals(CustomerTypeEnums.CUST_MERCHANT.getCode())){
				MerchantAccount queryMerchantAccount=new MerchantAccount();
				queryMerchantAccount.setPlatformId(unFreAndOutAccountRequest.getPlatformId());
				queryMerchantAccount.setMerchantId(unFreAndOutAccountRequest.getMerchantId());
				queryMerchantAccount.setMerchantType((unFreAndOutAccountRequest.getMerchantType()));
				queryMerchantAccount.setAccountType(unFreAndOutAccountRequest.getAccountType());
				MerchantAccount merchantAccount= merchantAccountMapper.selectOne(queryMerchantAccount);
				if(null!=merchantAccount&&merchantAccount.getStatus().equals(AccountStateEnums.normal.getCode())){
					if (!MD5.sign(CurrencyUtils.decimaltoString(merchantAccount.getAccountBalance()), balanceSaltValue, "utf-8").equals(merchantAccount.getCheckSum())) {
						response.setCode( BizResponseCode.MER_ACC_BALANCE_NOT_MATE.getCode());
						response.setMessage( BizResponseCode.MER_ACC_BALANCE_NOT_MATE.getMessage());
						log.info("个人或者商户解冻并出账接口-返回参数。。。。。。{}", response);
						return response;
					}
					if(merchantAccount.getFreezeBalance().compareTo(new BigDecimal(unFreAndOutAccountRequest.getUnFreezeAmt()))<0){//商户账户余额不足
						response.setCode( BizResponseCode.MER_ACC_BALANCE_NOT_ENOUGH.getCode());
						response.setMessage( BizResponseCode.MER_ACC_BALANCE_NOT_ENOUGH.getMessage());
						log.info("个人或者商户解冻并出账接口-返回参数。。。。。。{}", response);
						return response;
					}
					
					merchantAccount.setFreezeBalance(merchantAccount.getFreezeBalance().add(new BigDecimal(unFreAndOutAccountRequest.getUnFreezeAmt())));
//					merchantAccount.setCheckSum(MD5.sign(merchantAccount.getAccountBalance().toString(), balanceSaltValue, "utf-8"));
					merchantAccountMapper.updateByPrimaryKeySelective(merchantAccount);
				}else{
					log.info("个人或者商户解冻并出账接口--商户账户不存在或者被账户被冻结");
					response.setCode( BizResponseCode.MER_ACC_NOTEXIST.getCode());
					response.setMessage( BizResponseCode.MER_ACC_NOTEXIST.getMessage());
					return response;
				}
				response.setData(merchantAccount.getAccountId());
			}
			/**
			 * 客户类型-若是个人 : 则需要个人账户余额增加、新增个人账户流水记录
			 * */
			if(unFreAndOutAccountRequest.getCustomerType().equals(CustomerTypeEnums.CUST_PERSONAL.getCode())){
				PersonalAccount queryPersonalAccount = new PersonalAccount();
				queryPersonalAccount.setPlatformId(unFreAndOutAccountRequest.getPlatformId());
				queryPersonalAccount.setPersonalId(unFreAndOutAccountRequest.getPersonalId());
				queryPersonalAccount.setAccountType(unFreAndOutAccountRequest.getAccountType());
				PersonalAccount personalAccount = personalAccountMapper.selectOne(queryPersonalAccount);
				if(null!=personalAccount&&personalAccount.getStatus().equals(AccountStateEnums.normal.getCode())){
					if(!MD5.sign(CurrencyUtils.decimaltoString(personalAccount.getAccountBalance()), balanceSaltValue, "utf-8").equals(personalAccount.getCheckSum())){
						log.info("个人或者商户解冻并出账接口--个人账户余额不匹配");
						response.setCode( BizResponseCode.PER_ACC_BALANCE_NOT_MATE.getCode());
						response.setMessage( BizResponseCode.PER_ACC_BALANCE_NOT_MATE.getMessage());
						log.info("个人或者商户解冻并出账接口。。。。。。{}", response);
						return response;
					}
					personalAccount.setFreezeBalance(personalAccount.getFreezeBalance().add(new BigDecimal(unFreAndOutAccountRequest.getUnFreezeAmt())));
					personalAccountMapper.updateByPrimaryKeySelective(personalAccount);
				}else{
					log.info("个人或者商户解冻并出账接口--个人账户不存在或者被冻结");
					response.setCode( BizResponseCode.PER_ACC_NOTEXIST.getCode());
					response.setMessage( BizResponseCode.PER_ACC_NOTEXIST.getMessage());
					return response;
				}
				response.setData(personalAccount.getAccountId());
			}
			response.setCode( BizResponseCode.OK.getCode());
			response.setMessage( BizResponseCode.OK.getMessage());
		} catch (Exception e) {
			e.printStackTrace();
			log.info("个人或者商户解冻并出账接口-报错--"+e.getMessage());
			response.setCode( BizResponseCode.SERVICE_NOT_AVALIABLE.getCode());
			response.setMessage( BizResponseCode.SERVICE_NOT_AVALIABLE.getMessage());
			return response;
		}
		log.info("cancel 个人或者商户解冻并出账接口-结束：{}" + response);
		return response;
	}
	
	/**
	 * 解冻记录流水 try 
	 * @param unfreezeJournal
	 * @return
	 */
	@Hmily(confirmMethod="unfreAndOutJournalConfirm",cancelMethod="unfreAndOutJournalCancel")
	public BaseApiResponse insertUnfreezeJournal(UnfreezeJournal unfreezeJournal){
		log.info("try 记录解冻流水-请求参数：{}" + EntityUtils.beanToMap(unfreezeJournal));
		BaseApiResponse response = new BaseApiResponse();
		unfreezeJournal.setTccFlag("1");//生效数据
		int i=unfreezeJournalMapper.insertSelective(unfreezeJournal);
		if(i>0){
			response.setCode( BizResponseCode.OK.getCode());
			response.setMessage( BizResponseCode.OK.getMessage());
		}else{
			log.info("try插入解冻流水-异常：插入数据库报错--{}" + EntityUtils.beanToMap(unfreezeJournal));
			response.setCode( BizResponseCode.SERVICE_NOT_AVALIABLE.getCode());
			response.setMessage( BizResponseCode.SERVICE_NOT_AVALIABLE.getMessage());
		}
		log.info("try 记录解冻流水-结束：{}" + response);
		return response;
	}
	
	/**
	 * 记录解冻流水表 confirm 不做任何操作
	 * @param unfreezeJournal
	 * @return
	 */
	public BaseApiResponse unfreAndOutJournalConfirm(UnfreezeJournal unfreezeJournal) {
		log.info("confirm 记录解冻流水-请求参数：{}" + EntityUtils.beanToMap(unfreezeJournal));
		BaseApiResponse response = new BaseApiResponse();
		response.setCode( BizResponseCode.OK.getCode());
		response.setMessage(BizResponseCode.OK.getMessage());
		log.info("confirm 记录解冻流水-结束：{}" + response);
		return response;
	}
	
	/**
	 * 记录解冻流水表 cancel 将流水状态置为失效
	 * @param unfreezeJournal
	 * @return
	 */
	public BaseApiResponse unfreAndOutJournalCancel(UnfreezeJournal unfreezeJournal){
		log.info("cancel 记录解冻流水-请求参数：{}" + EntityUtils.beanToMap(unfreezeJournal));
		BaseApiResponse response = new BaseApiResponse();
		unfreezeJournal.setTccFlag("0");//无效数据
		unfreezeJournal.setUpdateTime(new Date());
		int i=unfreezeJournalMapper.updateByPrimaryKeySelective(unfreezeJournal);
		if(i==1){
			response.setCode(  BizResponseCode.OK.getCode());
			response.setMessage( BizResponseCode.OK.getMessage());
		}else{
			log.info("cancel 记录解冻流水-异常：修改数据库报错--{}"+ EntityUtils.beanToMap(unfreezeJournal));
			response.setCode(  BizResponseCode.SYSTEM_ERROR.getCode());
			response.setMessage( BizResponseCode.SYSTEM_ERROR.getMessage());
		}
		log.info("cancel 记录解冻流水-结束：{}" + response);
		return response;
	}
	
	/**
	 * 记录商户账户流水 try 
	 * @param merAccountJournal
	 * @return
	 */
	@Hmily(confirmMethod="merAccountJournalConfirm",cancelMethod="merAccountJournalCancel")
	public BaseApiResponse insertMerActJournal(MerchantAccountJournal merAccountJournal){
		
		log.info("try 记录商户账户流水-请求参数：{}" + EntityUtils.beanToMap(merAccountJournal));
		BaseApiResponse response = new BaseApiResponse();
		merAccountJournal.setTccFlag("1");//生效数据
		int i=merchantAccountJournalMapper.insertSelective(merAccountJournal);
		if(i>0){
			response.setCode( BizResponseCode.OK.getCode());
			response.setMessage( BizResponseCode.OK.getMessage());
		}else{
			log.info("try 记录商户账户流水-异常：插入数据库报错--{}" + EntityUtils.beanToMap(merAccountJournal));
			response.setCode( BizResponseCode.SERVICE_NOT_AVALIABLE.getCode());
			response.setMessage( BizResponseCode.SERVICE_NOT_AVALIABLE.getMessage());
		}
		log.info("try 记录商户账户流水-结束：{}" + response);
		return response;
	}
	
	/**
	 * 记录商户账户流水 confirm 不做任何操作
	 * @param merAccountJournal
	 * @return
	 */
	public BaseApiResponse merAccountJournalConfirm(MerchantAccountJournal merAccountJournal) {
		log.info("confirm 记录商户账户流水-请求参数：{}" + EntityUtils.beanToMap(merAccountJournal));
		BaseApiResponse response = new BaseApiResponse();
		response.setCode( BizResponseCode.OK.getCode());
		response.setMessage(BizResponseCode.OK.getMessage());
		log.info("confirm 记录商户账户流水-结束：{}" + response);
		return response;
	}
	
	/**
	 * 记录商户账户流水 cancel 将流水状态置为失效
	 * @param merAccountJournal
	 * @return
	 */
	public BaseApiResponse merAccountJournalCancel(MerchantAccountJournal merAccountJournal){
		log.info("cancel 记录商户账户流水-请求参数：{}" + EntityUtils.beanToMap(merAccountJournal));
		BaseApiResponse response = new BaseApiResponse();
		merAccountJournal.setTccFlag("0");//无效数据
		merAccountJournal.setUpdateTime(new Date());
		int i=merchantAccountJournalMapper.updateByPrimaryKeySelective(merAccountJournal);
		if(i==1){
			response.setCode(BizResponseCode.OK.getCode());
			response.setMessage(BizResponseCode.OK.getMessage());
		}else{
			log.info("cancel 记录商户账户流水-异常：修改数据库报错--{}"+ EntityUtils.beanToMap(merAccountJournal));
			response.setCode(BizResponseCode.SYSTEM_ERROR.getCode());
			response.setMessage(BizResponseCode.SYSTEM_ERROR.getMessage());
		}
		log.info("cancel 记录商户账户流水-结束：{}" + response);
		return response;
	}
	
	/**
	 * 记录个人账户流水 try 
	 * @param perAccountJournal
	 * @return
	 */
	@Hmily(confirmMethod="perAccountJournalConfirm",cancelMethod="perAccountJournalCancel")
	public BaseApiResponse insertPerActJournal(PersonalAccountJournal personalAccountJournal){
		
		log.info("try 记录商户账户流水-请求参数：{}" + EntityUtils.beanToMap(personalAccountJournal));
		BaseApiResponse response = new BaseApiResponse();
		personalAccountJournal.setTccFlag("1");//生效数据
		int i=personalAccountJournalMapper.insertSelective(personalAccountJournal);
		if(i>0){
			response.setCode( BizResponseCode.OK.getCode());
			response.setMessage( BizResponseCode.OK.getMessage());
		}else{
			log.info("try 记录商户账户流水-异常：插入数据库报错--{}" + EntityUtils.beanToMap(personalAccountJournal));
			response.setCode( BizResponseCode.SERVICE_NOT_AVALIABLE.getCode());
			response.setMessage( BizResponseCode.SERVICE_NOT_AVALIABLE.getMessage());
		}
		log.info("try 记录商户账户流水-结束：{}" + response);
		return response;
	}
	
	/**
	 * 记录个人账户流水 confirm 不做任何操作
	 * @param personalAccountJournal
	 * @return
	 */
	public BaseApiResponse perAccountJournalConfirm(PersonalAccountJournal personalAccountJournal) {
		log.info("confirm 记录个人账户流水-请求参数：{}" + EntityUtils.beanToMap(personalAccountJournal));
		BaseApiResponse response = new BaseApiResponse();
		response.setCode( BizResponseCode.OK.getCode());
		response.setMessage(BizResponseCode.OK.getMessage());
		log.info("confirm 记录个人账户流水-结束：{}" + response);
		return response;
	}
	
	/**
	 * 记录个人账户流水 cancel 将流水状态置为失效
	 * @param personalAccountJournal
	 * @return
	 */
	public BaseApiResponse perAccountJournalCancel(PersonalAccountJournal personalAccountJournal){
		log.info("cancel 记录商户账户流水-请求参数：{}" + EntityUtils.beanToMap(personalAccountJournal));
		BaseApiResponse response = new BaseApiResponse();
		personalAccountJournal.setTccFlag("0");//无效数据
		personalAccountJournal.setUpdateTime(new Date());
		int i=personalAccountJournalMapper.updateByPrimaryKeySelective(personalAccountJournal);
		if(i==1){
			response.setCode(  BizResponseCode.OK.getCode());
			response.setMessage( BizResponseCode.OK.getMessage());
		}else{
			log.info("cancel 记录商户账户流水-异常：修改数据库报错--{}"+ EntityUtils.beanToMap(personalAccountJournal));
			response.setCode(  BizResponseCode.SYSTEM_ERROR.getCode());
			response.setMessage( BizResponseCode.SYSTEM_ERROR.getMessage());
		}
		log.info("cancel 记录商户账户流水-结束：{}" + response);
		return response;
	}
	
	/**
	 * 校验冻结流水号是否被解冻
	 * @param unFreezeRequest
	 * @return
	 */
	public BaseApiResponse verificationFreezeId(UnFreezeAndOutAccountRequest unFreAndOutAccountRequest){
		log.info("校验冻结流水号是否被解冻 -- 请求参数：{}"+EntityUtils.beanToMap(unFreAndOutAccountRequest));
		BaseApiResponse response = new BaseApiResponse();
		UnfreezeJournal queryUnfreezeJournal = new UnfreezeJournal();
		queryUnfreezeJournal.setFreezeId(unFreAndOutAccountRequest.getFreezeId());
		UnfreezeJournal unfreezeJournal=unfreezeJournalMapper.selectOne(queryUnfreezeJournal);
		if(null!=unfreezeJournal){
			log.info("个人或者商户解冻并出账接口--冻结资金已解冻");
			response.setCode(BizResponseCode.UNFREEZE_IS_EXIST.getCode());
			response.setMessage(BizResponseCode.UNFREEZE_IS_EXIST.getMessage());
		}else{
			log.info("个人或者商户解冻并出账接口--冻结资金未解冻");
			response.setCode(BizResponseCode.OK.getCode());
			response.setMessage(BizResponseCode.OK.getMessage());
		}
		log.info("校验冻结流水号是否被解冻--结束：{}" + response);
		return response;
	}
	
	/**
	 * 个人或者商户直接出账接口
	 * 若是商户:
	 * 1.商户账户余额扣除，2.新增商户账户流水
	 * 若是个人:
	 * 1.个人账户余额扣除  2.新增个人账户流水
	 */
	@Hmily(confirmMethod="outAccountConfirm",cancelMethod="outAccountCancel")
	public BaseApiResponse outAccountTcc(OutAccountRequest outAccountRequest) {
		log.info("try 个人或者商户直接出账接口-请求参数：{}" + EntityUtils.beanToMap(outAccountRequest));
		BaseApiResponse response = new BaseApiResponse();
		try {
			if(outAccountRequest.getCustomerType().equals(CustomerTypeEnums.CUST_MERCHANT.getCode())){
				/**
				 * 修改商户账户余额
				 * */
				MerchantAccount queryMerchantAccount=new MerchantAccount();
				queryMerchantAccount.setPlatformId(outAccountRequest.getPlatformId());
				queryMerchantAccount.setMerchantId(outAccountRequest.getMerchantId());
				queryMerchantAccount.setMerchantType(outAccountRequest.getMerchantType());
				queryMerchantAccount.setAccountType(outAccountRequest.getAccountType());
				MerchantAccount merchantAccount= merchantAccountMapper.selectOne(queryMerchantAccount);
				if(null!=merchantAccount&&merchantAccount.getStatus().equals(AccountStateEnums.normal.getCode())){
					//校验商户账户余额是否匹配  和商户账户余额是否充足
					if(!MD5.sign(CurrencyUtils.decimaltoString(merchantAccount.getAccountBalance()), balanceSaltValue, "utf-8").equals(merchantAccount.getCheckSum())){
						response.setCode(BizResponseCode.MER_ACC_BALANCE_NOT_MATE.getCode());
						response.setMessage(BizResponseCode.MER_ACC_BALANCE_NOT_MATE.getMessage());
						log.info("个人或者商户直接出账接口 返回参数。。。。。。{}", response);//
						return response;
					}
					if(merchantAccount.getAccountBalance().compareTo(new BigDecimal(outAccountRequest.getOutAccAmt())) < 0){
						response.setCode(BizResponseCode.MER_ACC_BALANCE_NOT_ENOUGH.getCode());
						response.setMessage(BizResponseCode.MER_ACC_BALANCE_NOT_ENOUGH.getMessage());
						log.info("个人或者商户直接出账接口 返回参数。。。。。。{}", response);
						return response;
					}
					BigDecimal newBlance = merchantAccount.getAccountBalance().subtract(new BigDecimal(outAccountRequest.getOutAccAmt()));
					merchantAccount.setAccountBalance(newBlance);
					merchantAccount.setCheckSum(MD5.sign(CurrencyUtils.decimaltoString(newBlance), balanceSaltValue, "utf-8"));
					merchantAccountMapper.updateByPrimaryKeySelective(merchantAccount);
				}else{
					log.info("个人或者商户直接出账接口--商户账户不存在或者被冻结");
					response.setCode(BizResponseCode.MER_ACC_NOTEXIST.getCode());
					response.setMessage(BizResponseCode.MER_ACC_NOTEXIST.getMessage());
					return response;
				}
			}
			
			if(outAccountRequest.getCustomerType().equals(CustomerTypeEnums.CUST_PERSONAL.getCode())){
				PersonalAccount queryPersonalAccount = new PersonalAccount();
				queryPersonalAccount.setPlatformId(outAccountRequest.getPlatformId());
				queryPersonalAccount.setPersonalId(outAccountRequest.getPersonalId());
				queryPersonalAccount.setAccountType(outAccountRequest.getAccountType());
				PersonalAccount personalAccount = personalAccountMapper.selectOne(queryPersonalAccount);
				if(null!=personalAccount&&personalAccount.getStatus().equals(AccountStateEnums.normal.getCode())){
					if(!MD5.sign(CurrencyUtils.decimaltoString(personalAccount.getAccountBalance()), balanceSaltValue, "utf-8").equals(personalAccount.getCheckSum())){
						response.setCode(BizResponseCode.PER_ACC_BALANCE_NOT_MATE.getCode());
						response.setMessage(BizResponseCode.PER_ACC_BALANCE_NOT_MATE.getMessage());
						log.info("个人或者商户直接出账接口 返回参数。。。。。。{}", response);
						return response;
					}
					personalAccount.setAccountBalance(personalAccount.getAccountBalance().subtract(new BigDecimal(outAccountRequest.getOutAccAmt())));
					personalAccount.setCheckSum(MD5.sign(CurrencyUtils.decimaltoString(personalAccount.getAccountBalance()), balanceSaltValue, "utf-8"));
					personalAccountMapper.updateByPrimaryKeySelective(personalAccount);
				}else{
					log.info("个人或者商户直接出账接口--个人账户不存在或者被冻结");
					response.setCode(BizResponseCode.PER_ACC_NOTEXIST.getCode());
					response.setMessage(BizResponseCode.PER_ACC_NOTEXIST.getMessage());
					return response;
				}
			}
			response.setCode(BizResponseCode.OK.getCode());
			response.setMessage(BizResponseCode.OK.getMessage());
		} catch (Exception e) {
			e.printStackTrace();
			log.info("个人或者商户直接出账接口 报错--"+e.getMessage());
			response.setCode(BizResponseCode.SERVICE_NOT_AVALIABLE.getCode());
			response.setMessage(BizResponseCode.SERVICE_NOT_AVALIABLE.getMessage());
			return response;
		}
		log.info("try 个人或者商户直接出账接口-结束：{}" + response);
		return response;
	}
	
	/**
	 * 直接出账confirm 不做任何操作
	 * @param outAccountRequest
	 * @return
	 */
	public BaseApiResponse outAccountConfirm(OutAccountRequest outAccountRequest){
		log.info("confirm 直接出账接口开始-请求参数：{}" + EntityUtils.beanToMap(outAccountRequest));
		BaseApiResponse response = new BaseApiResponse();
		response.setCode( BizResponseCode.OK.getCode());
		response.setMessage( BizResponseCode.OK.getMessage());
		log.info("confirm 直接出账接口结束-结果：{}" + response);
		return response;
	}
	
	/**
	 * 出账接口 cancel
	 * 个人或者商户出账接口
	 * 若是商户：
	 * 1.商户账户金额增加
	 * 若是个人：
	 * 1.个人账户金额增加
	 */
	public BaseApiResponse outAccountCancel(OutAccountRequest outAccountRequest){
		log.info("cancel 个人或者商户直接出账接口-请求参数：{}" + EntityUtils.beanToMap(outAccountRequest));
		BaseApiResponse response = new BaseApiResponse();
		try {
			if(outAccountRequest.getCustomerType().equals(CustomerTypeEnums.CUST_MERCHANT.getCode())){
				/**
				 * 修改商户账户余额
				 * */
				MerchantAccount queryMerchantAccount=new MerchantAccount();
				queryMerchantAccount.setPlatformId(outAccountRequest.getPlatformId());
				queryMerchantAccount.setMerchantId(outAccountRequest.getMerchantId());
				queryMerchantAccount.setMerchantType(outAccountRequest.getMerchantType());
				queryMerchantAccount.setAccountType(outAccountRequest.getAccountType());
				MerchantAccount merchantAccount= merchantAccountMapper.selectOne(queryMerchantAccount);
				if(null!=merchantAccount&&merchantAccount.getStatus().equals(AccountStateEnums.normal.getCode())){
					//校验商户账户余额是否匹配  和商户账户余额是否充足
					if(!MD5.sign(CurrencyUtils.decimaltoString(merchantAccount.getAccountBalance()), balanceSaltValue, "utf-8").equals(merchantAccount.getCheckSum())){
						response.setCode(BizResponseCode.MER_ACC_BALANCE_NOT_MATE.getCode());
						response.setMessage(BizResponseCode.MER_ACC_BALANCE_NOT_MATE.getMessage());
						log.info("个人或者商户直接出账接口 返回参数。。。。。。{}", response);//
						return response;
					}
					if(merchantAccount.getAccountBalance().compareTo(new BigDecimal(outAccountRequest.getOutAccAmt())) < 0){
						response.setCode(BizResponseCode.MER_ACC_BALANCE_NOT_ENOUGH.getCode());
						response.setMessage(BizResponseCode.MER_ACC_BALANCE_NOT_ENOUGH.getMessage());
						log.info("个人或者商户直接出账接口 返回参数。。。。。。{}", response);
						return response;
					}
					BigDecimal newBlance = merchantAccount.getAccountBalance().add(new BigDecimal(outAccountRequest.getOutAccAmt()));
					merchantAccount.setAccountBalance(newBlance);
					merchantAccount.setCheckSum(MD5.sign(CurrencyUtils.decimaltoString(newBlance), balanceSaltValue, "utf-8"));
					merchantAccountMapper.updateByPrimaryKeySelective(merchantAccount);
				}else{
					log.info("个人或者商户直接出账接口--商户账户不存在或者被冻结");
					response.setCode(BizResponseCode.MER_ACC_NOTEXIST.getCode());
					response.setMessage(BizResponseCode.MER_ACC_NOTEXIST.getMessage());
					return response;
				}
			}
			
			if(outAccountRequest.getCustomerType().equals(CustomerTypeEnums.CUST_PERSONAL.getCode())){
				PersonalAccount queryPersonalAccount = new PersonalAccount();
				queryPersonalAccount.setPlatformId(outAccountRequest.getPlatformId());
				queryPersonalAccount.setPersonalId(outAccountRequest.getPersonalId());
				queryPersonalAccount.setAccountType(outAccountRequest.getAccountType());
				PersonalAccount personalAccount = personalAccountMapper.selectOne(queryPersonalAccount);
				if(null!=personalAccount&&personalAccount.getStatus().equals(AccountStateEnums.normal.getCode())){
					if(!MD5.sign(CurrencyUtils.decimaltoString(personalAccount.getAccountBalance()), balanceSaltValue, "utf-8").equals(personalAccount.getCheckSum())){
						response.setCode(BizResponseCode.PER_ACC_BALANCE_NOT_MATE.getCode());
						response.setMessage(BizResponseCode.PER_ACC_BALANCE_NOT_MATE.getMessage());
						log.info("个人或者商户直接出账接口 返回参数。。。。。。{}", response);
						return response;
					}
					personalAccount.setAccountBalance(personalAccount.getAccountBalance().add(new BigDecimal(outAccountRequest.getOutAccAmt())));
					personalAccount.setCheckSum(MD5.sign(CurrencyUtils.decimaltoString(personalAccount.getAccountBalance()), balanceSaltValue, "utf-8"));
					personalAccountMapper.updateByPrimaryKeySelective(personalAccount);
				}else{
					log.info("个人或者商户直接出账接口--个人账户不存在或者被冻结");
					response.setCode(BizResponseCode.PER_ACC_NOTEXIST.getCode());
					response.setMessage(BizResponseCode.PER_ACC_NOTEXIST.getMessage());
					return response;
				}
			}
			response.setCode(BizResponseCode.OK.getCode());
			response.setMessage(BizResponseCode.OK.getMessage());
		} catch (Exception e) {
			e.printStackTrace();
			log.info("个人或者商户直接出账接口 报错--"+e.getMessage());
			response.setCode(BizResponseCode.SERVICE_NOT_AVALIABLE.getCode());
			response.setMessage(BizResponseCode.SERVICE_NOT_AVALIABLE.getMessage());
			return response;
		}
		log.info("cancel 个人或者商户直接出账接口-结束：{}" + response);
		return response;
	
	}
	
	/**
	 * 解冻并出账
	 * @author centerroot
	 * @time 创建时间:2018年11月2日下午3:44:18
	 * @param unFreezeRequest
	 * @return
	 */
	public BaseApiResponse unfreezeAndTransfer(UnFreezeAndTransferRequest unFreezeAndTransferRequest) {
		BaseApiResponse response = new BaseApiResponse();
		UnFreezeAndOutAccountRequest unFreAndOutAccountRequest = new UnFreezeAndOutAccountRequest();
		IntoAccountRequest intoAccountRequest = new IntoAccountRequest();

		BeanUtils.copyProperties(unFreezeAndTransferRequest, unFreAndOutAccountRequest);
		BeanUtils.copyProperties(unFreezeAndTransferRequest, intoAccountRequest);
		unFreAndOutAccountRequest.setUnFreezeAmt(unFreezeAndTransferRequest.getTradeAmt());
		if (CustomerTypeEnums.CUST_MERCHANT.getCode().equals(unFreezeAndTransferRequest.getCustomerType())) {
			intoAccountRequest.setCustomerType(CustomerTypeEnums.CUST_PERSONAL.getCode());
		} else {
			intoAccountRequest.setCustomerType(CustomerTypeEnums.CUST_MERCHANT.getCode());
		}
		response = unFreAndOutAccount(unFreAndOutAccountRequest);
		if (BizResponseCode.OK.getCode().equals(response.getCode())) {
//			response = commonIntoAccountBiz.intoAccount(intoAccountRequest);
		} else {
			throw new RuntimeException();
		}
		log.info("解冻并出账:{}",response);
		return response;
		
	}
}
